package com.lineage.server.serverpackets;

import com.add.system.L1FireCrystal;
import com.add.system.L1FireSmithCrystalTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1PcInventory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class S_ShopBuyListFireSmith extends ServerBasePacket {
  private byte[] _byte = null;
  
  public S_ShopBuyListFireSmith(L1PcInstance pc, L1NpcInstance npc) {
    Map<L1ItemInstance, Integer> assessedItems = assessItems(pc.getInventory());
    if (assessedItems.isEmpty()) {
      pc.sendPackets(new S_NPCTalkReturn(pc.getId(), "smithitem3"));
      return;
    } 
    if (assessedItems.size() <= 0) {
      pc.sendPackets(new S_NoSell(npc));
      return;
    } 
    writeC(65);
    writeD(npc.getId());
    writeH(assessedItems.size());
    Iterator<L1ItemInstance> iterator = assessedItems.keySet().iterator();
    while (iterator.hasNext()) {
      L1ItemInstance key = iterator.next();
      writeD(key.getId());
      writeD(((Integer)assessedItems.get(key)).intValue());
    } 
    writeH(7);
  }
  
  private Map<L1ItemInstance, Integer> assessItems(L1PcInventory inv) {
    Map<L1ItemInstance, Integer> result = new HashMap<>();
    Iterator<L1ItemInstance> iterator = inv.getItems().iterator();
    while (iterator.hasNext()) {
      L1ItemInstance item = iterator.next();
      switch (item.getItem().getItemId()) {
        case 40308:
        case 40314:
        case 40316:
        case 41246:
        case 44070:
        case 80033:
        case 83000:
        case 83022:
          continue;
      } 
      if (item.isEquipped() || item.getItem().getType2() == 0)
        continue; 
      int key = item.getItemId();
      if (item.getBless() == 0) {
        key = item.getItemId() - 100000;
      } else if (item.getBless() == 2) {
        key = item.getItemId() - 200000;
      } 
      L1FireCrystal firecrystal = L1FireSmithCrystalTable.get().getTemplate(key);
      if (firecrystal == null)
        continue; 
      int crystalcount = firecrystal.get_CrystalCount(item);
      if (crystalcount > 0)
        result.put(item, Integer.valueOf(crystalcount)); 
    } 
    return result;
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
